async function syncFetch(url) {
  const response = await fetch(url);
  const data = await response.json();
  return data;
}

async function run() {
  showOverlay();
  let shipmentId = location.href.split('/')[6]
  let total = 1;
  let index = 0;
  let allItems = []

  while(total > index) {
    let detail_url = `${location.origin}/fba/wmsr/discrepancies/${shipmentId}?filter=NONE&index=${index}&pageSize=50&isOrderAscending=true&orderBy=MSKU`

    let detail = await syncFetch(detail_url);
    let result = detail.items.map(it => {
      return {
        merchantSKU: it.merchantSKU,
        expectedQuantity: it.expectedQuantity
      };
    });
    allItems.push(result)

    total = detail.totalRecords
    index = index + 50

    chrome.runtime.sendMessage( { action: 'openUrlAndExecScript', data: allItems.flat() } );
  }
}

run()
